package com.servlet;

import com.service.QRService;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by ztc on 16-3-21.下午3:53
 */
public class QRAjax extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request,response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session=request.getSession();
        QRService service=new QRService();
        PrintWriter out = response.getWriter();
        //计时
        int time=0;
        if(session!=null){
            //长轮询
            while(true) {
                time++;
                if (service.isReflash()) {
                    out.print("No. "+session.getAttribute("recordCount")+" ");
                    break;
                }else{
                    if(time>=10){
                        out.print("failed");
                        break;
                    }else {
                        try {
                            //保持住
                            Thread.sleep(1000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                }
            }
        }
        out.close();
    }
}//不明觉厉

